﻿#include "animationStateRescue.h"


/** @brief 构造函数 */
AnimationStateRescue::AnimationStateRescue()
{
//    QString sql = "create table AnimationState( "
//                  "ID INTEGER Primary Key AutoIncrement,"
//                  "PartName TEXT,"
//                  "State    INT,"
//                  "Module   INT,"
//                  "Step     INT)";

}


/** @brief 单例函数 */
AnimationStateRescue& AnimationStateRescue::I(){
    static AnimationStateRescue instance;
    return instance;
}

/** @brief 解析数据库指令
 *  @param stepIdx 步骤编号  */
void AnimationStateRescue::parseDBInstruct(int stepIdx){

    __LOG_FSTART(stepIdx);

    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql = "select info, step from experimentDetail order by step";
    query.exec(sql);

    AnimationManager_Purify::I()._ctrl_equ->stopTimer();
    AnimationManager_Practice::I()._controller->stopTimer();
    AnimationManager_Shifting::I()._controller->stopTimer();

    while (query.next()) {

        if(query.value("step").toInt() >= stepIdx){
            break;
        }

        qDebug() << "解析指令" << query.value("step").toInt();
        AnimationParseHEX::I().parse(query.value("info").toString().split(" "));
        for(int i = 0; i < 100; i ++){
            AnimationManager_Purify::I()._ctrl_equ->updateState(1);
            AnimationManager_Practice::I()._controller->updateState(1);
            AnimationManager_Shifting::I()._controller->updateState(1);
        }
    }
    AnimationManager_Purify::I()._ctrl_equ->startTimer();
    AnimationManager_Practice::I()._controller->startTimer();
    AnimationManager_Shifting::I()._controller->startTimer();

    __LOG_FEND__;

}


/** @brief 获取实验恢复实验名称、耗材编码等信息
 *  @return QVariantMap[信息]=值   */
QVariantMap AnimationStateRescue::getRescueExpInfo(){

    __LOG_FSTART__;

    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql = "select * from reportinfo order by serial desc limit 1";
    query.exec(sql);

    QVariantMap res;
    while (query.next()) {
        res["MainMethod"] = "";
        res["SingleMethod1"] = "";
        res["SingleMethod2"] = "";
        res["SingleMethod3"] = "";
        res["SingleMethod4"] = "";

        res["ConsumableName"] = query.value("ConsumableName");
        res["ConsumableCode"] = query.value("ConsumableCode");
        res["ExpName"] = query.value("ExpName");

        QString expType = query.value("ExpType").toString();
        if(expType == "全流程"){
            res["ExpType"] = 0;
            res["MainMethod"] = query.value("ExpFlowName");
        }else if(expType == "放靶"){
            res["ExpType"] = 1;
            res["SingleMethod1"] = query.value("ExpFlowName");
        }else if(expType == "打靶"){
            res["ExpType"] = 2;
            res["SingleMethod2"] = query.value("ExpFlowName");
        }else if(expType == "转靶"){
            res["ExpType"] = 3;
            res["SingleMethod3"] = query.value("ExpFlowName");
        }else{
            res["ExpType"] = 4;
            res["SingleMethod4"] = query.value("ExpFlowName");
        }
        res["ExpDate"] = query.value("ExpDate");
        res["Operator"] = query.value("Operator");
        res["Memo"] = query.value("Memo");
    }

    __LOG_FEND(res);

    return res;

}

